package com.qf.utils;

import com.sun.org.apache.xpath.internal.operations.Mult;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ShibieAdd {
    public static String add(MultipartFile multipartFile, String accessToken, String phone) {
        byte[] bytes=null;
        try {
            bytes = multipartFile.getBytes();
        } catch (IOException e) {
            System.out.println("錯誤");
        }
        String encode = Base64Util.encode(bytes);
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add";
        System.out.println(encode);
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("image", encode);
            map.put("group_id", "test1");
            map.put("user_id", phone);
            map.put("user_info", "abc");
            map.put("image_type", "BASE64");
            String param = GsonUtils.toJson(map);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            /*String accessToken = "[调用鉴权接口获取的token]";*/

            String result = HttpUtil.post(url, accessToken, "application/json", param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
